﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Jumpton.SFA.Server.BusinessRule.MasterData;
using Jumpton.SFA.Server.Entity;
using Jumpton.SFA.Server.BusinessRule;

public partial class ExportOrder : PageBase
{

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GridPager1.GridView = gvChannelType;
            GridPager1.CurrentPage = PageIndex;
            InitForm();
        }
    }

    private void InitForm()
    {
        Jumpton.Persistent.ObjectTable ot;

        //经销商
        ddlChain.DataTextField = "DESCRIPTION";
        ddlChain.DataValueField = "CODE";
        ot = new ChainManager().GetCourseTable(this.MyStateManager.DomainID);
        if (ot != null)
        {
            ddlChain.DataSource = ot;//.FindByFilter("IsValid='true'");
            ddlChain.DataBind();
        }
        ddlChain.Items.Insert(0, new ListItem("--所有--", ""));

       
        DictionaryItemsManager dim = new DictionaryItemsManager();

        //区域层次
        ddlLevel.DataTextField = "Name";
        ddlLevel.DataValueField = "DictionaryItemId";
        ddlLevel.DataSource = dim.GetItemList(DictionaryEnum.ChannelType, this.MyStateManager.DomainID);
        ddlLevel.DataBind();
        ddlLevel.Items.Insert(0, new ListItem("--所有--", ""));
       
        //客户类别

        ddlClass.DataTextField = "Name";
        ddlClass.DataValueField = "DictionaryItemId";
        ddlClass.DataSource = dim.GetItemList(DictionaryEnum.CustomerClass, this.MyStateManager.DomainID);
        ddlClass.DataBind();
        ddlClass.Items.Insert(0, new ListItem("--所有--", ""));

        //销售渠道
        ddlChannel.DataTextField = "Name";
        ddlChannel.DataValueField = "DictionaryItemId";
        ddlChannel.DataSource = dim.GetItemList(DictionaryEnum.CustomerChannel, this.MyStateManager.DomainID);
        ddlChannel.DataBind();
        ddlChannel.Items.Insert(0, new ListItem("--所有--", ""));

        //巡店督导
        DataTable dt = new Jumpton.SFA.Server.BusinessRule.MobileUserManager().Load("", this.MyStateManager.DomainID,this.MyStateManager.OrgID);
        ddlUser.DataTextField = "CODENAME";
        ddlUser.DataValueField = "ID";
        ddlUser.DataSource = dt;
        ddlUser.DataBind();
        ddlUser.Items.Insert(0, new ListItem("--所有--", ""));

        //绑定订单状态
        string[] names = Enum.GetNames(typeof(Jumpton.SFA.Server.Entity.EOderStatus));
        int[] values = (int[])Enum.GetValues(typeof(Jumpton.SFA.Server.Entity.EOderStatus));
        for (int i = 0; i < names.Length; i++)
        {
            ddlStatus.Items.Add(new ListItem(names[i], values[i].ToString()));
        }
        ddlStatus.Items.Insert(0, new ListItem("--所有--", ""));

    }

    private void BindGrid()
    {
        System.Text.StringBuilder sb = new System.Text.StringBuilder(" ");


        if (ddlChain.SelectedValue != string.Empty)
        {
            sb.Append(" And C.CHAIN_CODE='" + ddlChain.SelectedValue + "' ");
        }
        if (ddlChannel.SelectedValue != string.Empty)
        {
            sb.Append(" AND C.LOCAL_LEVEL1_CODE =  '" + ddlChannel.SelectedValue + "'  ");
        }
        if (ddlClass.SelectedValue != string.Empty)
        {
            sb.Append(" AND C.CUST_CLASS_CODE =  '" + ddlClass.SelectedValue + "'  ");
        }
        if (ddlLevel.SelectedValue != string.Empty)
        {
            sb.Append(" AND C.GEO_LEVEL_CODE ='" + ddlLevel.SelectedValue + "' ");
        }
        if (ddlUser.SelectedValue != string.Empty)
        {
            sb.Append(" AND OH.USER_ID ='"+ddlUser.SelectedValue+"'  ");
        }
        if (txtCode.Text != string.Empty)
        {
            sb.Append(" AND (C.CODE LIKE '%"+txtCode.Text+"%' OR C.NAME LIKE '%"+txtCode.Text+"%')  ");
        }
        if (txtStart.Text!=string.Empty)
        {
            sb.Append(" AND DATEDIFF(DD,OH.ORDER_date,'"+DateTime.Parse(txtStart.Text).ToShortDateString()+"')<=0 ");
        }
        if (txtEnd.Text != string.Empty)
        {
            sb.Append(" AND DATEDIFF(DD,OH.ORDER_date,'"+DateTime.Parse(txtEnd.Text).ToShortDateString()+"')>=0 ");
        }
        if (ddlStatus.SelectedValue != string.Empty)
        {
            sb.Append(" AND  OH.STATUS='"+ddlStatus.SelectedValue+"'  ");
        }
        DataSet ds = OrderHeaderManager.SearchOrderList(MyStateManager.DomainID, sb.ToString());
        if (ds != null && ds.Tables.Count > 0)
        {
            GridPager1.DataSource = ds.Tables[0];
            GridPager1.BindData();
        }


        //this.gvChannelType.DataSource = CustomerManager.SearchCustomer(" AND C.[DOMAIN_ID] = '"+this.MyStateManager.DomainID+"' ");
        //this.gvChannelType.DataBind();
    }

    protected void btnExport_Click(object sender, EventArgs e)
    {
        string orderID = "";
        for (int i = 0; i < gvChannelType.Rows.Count; i++)
        {
            orderID=gvChannelType.DataKeys[i].Value.ToString();
            CheckBox ckbSelect = gvChannelType.Rows[i].FindControl("ckbSelected") as CheckBox;

            if (orderID!=""&&ckbSelect!=null&&ckbSelect.Checked)
            {
                OrderHeaderManager.ExportOrderToHistory(orderID, MyStateManager.Name, MyStateManager.DomainID);
            }
        }
        this.BindGrid();
        ShowMessage("导出成功！");
   }

     public override void VerifyRenderingInServerForm(Control control)
    {

        // Confirms that an HtmlForm control is rendered for the  specified ASP.NET server control at run time.

    }


    protected void btnQuery_Click(object sender, EventArgs e)
    {
        BindGrid();
    }

    public void GridViewToolbarUserControl1_OnGVTBSetColumnsButtonEvent(object sender, EventArgs e)
    {
        BindGrid();
    }
}
